package com.transfer.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;
import com.transfer.entity.TitleAppoint;

public class TitleAppointQueryDTO {
	private Long id;
	private String appointTitle;//聘任职称
	private Date appointDate;//聘任时间
	private String staffName;
	private String staffNum;
	
	private Long staffId;

	public TitleAppointQueryDTO(TitleAppoint ta,Staff staff) {
		super();
		this.id = ta.getId();
		this.appointTitle = ta.getAppointTitle();
		this.appointDate = ta.getAppointDate();
		this.staffName = ta.getStaffName();
		this.staffNum = ta.getStaffNum();
		this.staffId = staff.getId();
	}
	public TitleAppointQueryDTO() {
		super();
	}
	public Long getId() {
		return id;
	}
	public String getAppointTitle() {
		return appointTitle;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getAppointDate() {
		return appointDate;
	}
	public String getStaffName() {
		return staffName;
	}
	public String getStaffNum() {
		return staffNum;
	}
	public Long getStaffId() {
		return staffId;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setAppointTitle(String appointTitle) {
		this.appointTitle = appointTitle;
	}
	public void setAppointDate(Date appointDate) {
		this.appointDate = appointDate;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<TitleAppoint> getSpecification(TitleAppointQueryDTO userQueryDTO)
	{
		Specification<TitleAppoint> spec = new Specification<TitleAppoint>() {
			public Predicate toPredicate(Root<TitleAppoint> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffName())) {
					 Predicate  p1 =  cb.like(root.get("staffName").as(String.class),"%"+ userQueryDTO.getStaffName() + "%");
					 list.add(p1);
				 }
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffNum())) {
					 Predicate  p2 =  cb.like(root.get("staffNum").as(String.class),"%"+ userQueryDTO.getStaffNum() + "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
	
}
